package com.wasin.server.annotation;

import cn.dev33.satoken.stp.StpUtil;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.vo.Status;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;


public class UserIdResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //添加自定义注解
        if (methodParameter.hasParameterAnnotation(UserId.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        if(StpUtil.getLoginIdDefaultNull()==null){
            throw new BusinessException(Status.FAIL_INVALID_TOKEN, "请重新登录");
        }
        return StpUtil.getLoginIdAsLong();

    }
}
